stage.set_axis(100)
stage.create_grid_overlay(10, "grey")
stage.set_background("soccerfield")
ballx = -90
bally = random.randint(-100,100)
# sprite = codesters.Sprite("image", x, y)
ball = codesters.Sprite("soccerball", ballx, bally)
netx = 80
nety = random.randint(-100,100)
# sprite = codesters.Sprite("image", x, y)
net = codesters.Sprite("soccernet", netx, nety)
net.flip_right_left()
#sprite = codesters.Line(x-start, y-start, x-end, y-end, "color")
line = codesters.Line(ballx, bally, netx, nety, "blue")
slopeguess = float(line.ask("What is the slope of this line?"))
rise = nety - bally
run = netx - ballx
slope = rise / run
# sprite = codesters.Text("text", x, y)
sprite = codesters.Text(slope, 0, 30)
newrise = slopeguess*run
end_y = newrise + bally
ball.set_color("red")
ball.pen_down()
ball.glide_to(netx, end_y)
# percent_error = abs((slopeguess-slope)/slope*100)
# slopeguess = round(slopeguess, 2)
# slope = round(slope, 2)
rounded_error = round(abs(slopeguess-slope), 3)
score = False
topnet = nety + 38
bottomnet = nety - 35
blanktext = " "
endtext = codesters.Text(blanktext, 0, 15)
text1 = "The slope is positive and so was your guess!"
text1a = "The slope is negative and so was your guess!"
text1b = "The slope is positive and your guess was negative!"
text1c = "The slope is negative and your guess was positive!"
text2 = "But you were off by " + str(rounded_error) + "!"
text3 = "Click Run to try again!"
if bottomnet <= end_y <= topnet:
score = True
if score:
text = "Great job!"
endtext.set_text(text)
else:
text = "Hmmm..."
endtext = codesters.Text(text, 0, 15)
stage.wait(1)
if slope >=0 and slopeguess >=0:
endtext.set_text(blanktext)
endtext.set_text(text1)
stage.wait(1.5)
endtext.set_text(text2)
stage.wait(1.5)
endtext.set_text(text3)
if slope <0 and slopeguess <=0:
endtext.set_text(blanktext)
endtext.set_text(text1a)
stage.wait(1.5)
endtext.set_text(text2)
stage.wait(1.5)
endtext.set_text(text3)
if slope >= 0 and slopeguess <0:
endtext.set_text(blanktext)
endtext.set_text(text1b)
stage.wait(1.5)
endtext.set_text(text3)
if slope <=0 and slopeguess >0:
endtext.set_text(blanktext)
endtext.set_text(text1c)
stage.wait(1.5)
endtext.set_text(text3)
t1 = TestObjective()
t1.add_success(score, "Great Job!")
t1.add_failure(not score, "Try again! Can you make it in the net?")
tester = TestManager()
tester.add_test_list([t1])
tester.run_tests()
tester.display_first_feedback()
-
Run Code
-
Activity Submitted!
Submit Work
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)